<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class GoodsLeixing extends Base
{
    public function index(){
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('goods_leixing')->where($map)->count();

            $field = '*';
            $list = Db::name('goods_leixing')
                ->field($field)
                ->where($map)
                ->order('id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $list[$k] = $v;
            }

            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }
    public function edit(){
        $id = input('id');
        $info = [];
        if($id){
            $info = Db::name('goods_leixing')->where('id',$id)->find();
        }
        View::assign('info',$info);
        return View::fetch();
    }
    public function save(){
        $id = input('id');
        $name = input('name');
        if($name==''){
            return json(['status'=>0,'msg'=>'请填写分类名称']);
        }
        $map = [];
        $map['name'] = $name;
        if($id){
            $map['id'] = ['<>',$id];
        }
        $have = Db::name('goods_leixing')->where($map)->find();
        if($have && $have['id']!=$id){
            return json(['status'=>0,'msg'=>'分类名称已经存在']);
        }
        $data = [];
        $data['name'] = $name;
        if($id){
            $res = Db::name('goods_leixing')->where('id',$id)->update($data);
        }else{
            $res = Db::name('goods_leixing')->insert($data);
        }
        return json(['status'=>0,'msg'=>'操作成功']);
    }
}